package com.googlecode.jpocketmod;

import com.googlecode.jpocketmod.actions.ActionFactory;
import com.googlecode.jpocketmod.actions.ApplicationEventListener;
import com.googlecode.jpocketmod.actions.ApplicationExitingEvent;
import com.googlecode.jpocketmod.actions.TemplateLoadingEvent;
import javax.swing.*;

public class JPocketModApp implements ApplicationEventListener  {
	private JFrame frame;
	private TemplatePanel panel = new TemplatePanel();
	private static final long serialVersionUID = 1L;

	public JPocketModApp()  {
		setApplicationLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		
		frame = new JFrame();
		DialogManager.getInstance().setDialogOwner(frame);
		
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setTitle("JPocketMod");
		frame.setJMenuBar(new JPocketModAppMenubar());
		
		frame.getContentPane().add(panel);
		frame.pack();
		frame.setSize(420, 600);
		
		/*Register any application listeners*/
		ActionFactory.getInstance().getExitApplicationAction().addApplicationEventListener(this);
		
		frame.setVisible(true);
	}
	
	private void setApplicationLookAndFeel(String lookAndFeel)  {
		try {
			UIManager.setLookAndFeel(lookAndFeel);
		} catch (UnsupportedLookAndFeelException e) {
		} catch (ClassNotFoundException e) {
		} catch (InstantiationException e) {
		} catch (IllegalAccessException e) {
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new JPocketModApp();
	}

	public void applicationExiting(ApplicationExitingEvent ae) {
		ApplicationProperties.getInstance().saveProperties();
		
		System.exit(0);
	}
	
	public void templateLoading(TemplateLoadingEvent tle)  {
		panel.reload();
		frame.repaint();
	}
}
